/*******************************************************************************
 * This file is used in CS4233, Object-oriented Analysis and Design
 *
 * Copyright (c) 2011 Worcester Polytechnic Institute.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Author:
 *    gpollice
 *******************************************************************************/
package hw4;

import java.util.ArrayList;
import java.util.Collection;

/**
 * The BoardObserver observes a chess board. When an instance of this
 * class is notified of a move, it forwards that MoveEvent to all objects
 * observing it.
 * 
 * @author gpollice
 * @author Jon Marokhovsky
 * @version Oct 5, 2011
 */
public class BoardObserver implements Observable, Observer
{
	private ChessBoard observedBoard;
	private Object theEvent;
	private final Collection<Observer> observers;
	/**
	 * The constructor for this class is given a ChessBoard object to 
	 * observe.
	 * 
	 * @param observed the board to observe
	 */
	public BoardObserver(ChessBoard observed)
	{
		observedBoard = observed;
		observers = new ArrayList<Observer>();
	}
	
	/**
	 * This is a debugger method which verifies that the correct board is stored in the
	 * BoardObserver.
	 * @return board: the board being observed by the BoardObserver.
	 */
	protected ChessBoard getBoard()
	{
		return observedBoard;
	}
	
	/**
	 * This is a debugger method which verifies that the collection of observers is correct.
	 * @return observers: the collection of observers stored in BoardObserver.
	 */
	protected Collection<Observer> getObservers()
	{
		return observers;
	}
	
	/**
	 * This is a debugger method which verifies that the event is correctly passed between
	 * observers.
	 * @return theEvent: the object which this observer recieved as an event.
	 */
	protected Object getEvent()
	{
		return theEvent;
	}
	
	/*
	 * @see hw4.Observer#update(java.lang.Object, java.lang.Object)
	 */
	@Override
	public void update(Object subject, Object event)
	{
		final ChessBoard newBoard;
		if (subject instanceof ChessBoard)
		{
			newBoard = (ChessBoard) subject;
			observedBoard = newBoard;
		}
		if (subject instanceof BoardObserver) // this is just for debugging purposes
		{
			final BoardObserver bObserver = (BoardObserver) subject;
			observedBoard = bObserver.getBoard();
		}
		theEvent = event;
		notifyObservers();
	}

	/*
	 * @see hw4.Observable#addObserver(hw4.Observer)
	 */
	@Override
	public void addObserver(Observer observer)
	{
		observers.add(observer);

	}

	/*
	 * @see hw4.Observable#removeObserver(hw4.Observer)
	 */
	@Override
	public void removeObserver(Observer observer)
	{
		observers.remove(observer);
	}

	/*
	 * @see hw4.Observable#notifyObservers()
	 */
	@Override
	public void notifyObservers()
	{
		for (Observer o: observers)
		{
			o.update(this, theEvent);
		}
	}

}
